package com.hhly.rabbitmq;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class RabbitMQUtil {
	private static ConnectionFactory factory;

	private static Connection connection;

	static {
		factory = new ConnectionFactory();
		factory.setUsername("guest");
		factory.setPassword("guest");
		factory.setVirtualHost("/");
		factory.setHost("192.168.74.167");
		factory.setPort(5672);
	}

	public static Channel getChannel() throws IOException {
		if (connection == null) {
			synchronized (RabbitMQUtil.class) {
				if (connection == null) {
					try {
						ExecutorService es = Executors.newFixedThreadPool(10);
						connection = factory.newConnection(es);//创建连接可以指定线程池大小
						//connection = factory.newConnection();//消费者线程池默认10
					} catch (IOException | TimeoutException e) {
						e.printStackTrace();
					}
				}
			}

		}
		return connection.createChannel();
	}
}
